<?php

# http://projects:8081/cdapp-trex-api/api/query_annual_record_progress.php?traineeNumber=2012036
# http://projects:8081/cdapp-trex-api/api/query_annual_record_progress.php?traineeName=王涛
# http://projects:8081/cdapp-trex-api/api/query_annual_record_progress.php?office=管制二室
# http://projects:8081/cdapp-trex-api/api/query_annual_record_progress.php?traineeNumber=2012036&year=2022
# http://projects:8081/cdapp-trex-api/api/query_annual_record_progress.php?traineeName=王涛&year=2022
# http://projects:8081/cdapp-trex-api/api/query_annual_record_progress.php?office=管制二室&year=2022

include_once 'common.php';

$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
$office = isset($_GET['office']) ? $_GET['office'] : '';
$traineeNumber = isset($_GET['traineeNumber']) ? $_GET['traineeNumber'] : 0;
$traineeName = isset($_GET['traineeName']) ? $_GET['traineeName'] : '';

main($year, $office, $traineeNumber, $traineeName);

function main($year, $office, $traineeNumber, $traineeName) {
  $output = [];
  if ($traineeNumber !== 0) {
    $output['list'] = query_by_trainee($year, $traineeNumber, '', 0);
  } elseif ($traineeName !== '') {
    $output['list'] = query_by_trainee($year, 0, $traineeName, 0);
  } elseif ($office != '') {
    $trainee_list = query_trainee_list_by_office($office);
    $output['content'] = [];
    foreach ($trainee_list as $trainee) {
      $output_trainee = ['number' => $trainee['number'], 'name' => $trainee['name']];
      $output_trainee['list'] = query_by_trainee($year, $trainee['number'], '', 0);
      $output['content'][] = $output_trainee;
    }
  }
  $output['exercises'] = query_annual_exercises_by_office($year, $office, 0, []);
  output($output);
  return $output;
}

function query_trainee_list_by_office($office) {
  $sql = "SELECT `number`, `name` FROM `oprec_security_data` WHERE `clientOffice` = '{$office}' AND  `disposed` =  0";
  $result = exec_sql($sql);
  return $result ? $result : [];
}

function query_by_trainee($year, $traineeNumber = 0, $traineeName = '', $detail = 0) {
  $sql = "";
  $output = [];

  # 全部已做
  $sql = "SELECT `number`, `title` FROM `trex_records` WHERE (`traineeNumber` = '{$traineeNumber}' OR `traineeName` = '{$traineeName}') AND `year` = {$year}";
  $result = exec_sql($sql);
  $output['全部已做'] = $result ? $result : [];
  // if ($detail === 1) {
  //   $sql = "SELECT * FROM `trex_records` WHERE (`traineeNumber` = '{$traineeNumber}' OR `traineeName` = '{$traineeName}') AND `year` = {$year}";
  //   $output['全部已做_明细'] = $result ? $result : [];
  // }

  # 复训已做
  $sql = "SELECT `number`, `title` FROM `trex_records` WHERE (`traineeNumber` = '{$traineeNumber}' OR `traineeName` = '{$traineeName}') AND `type` = 1 AND `year` = {$year}";
  $result = exec_sql($sql);
  $output['复训已做'] = $result ? $result : [];

  # 复训已通过
  $sql = "SELECT `number`, `title` FROM `trex_records` WHERE (`traineeNumber` = '{$traineeNumber}' OR `traineeName` = '{$traineeName}') AND `type` = 1 AND `year` = {$year} AND `pass` = '是'";
  $result = exec_sql($sql);
  $output['复训已通过'] = $result ? $result : [];

  # 复训未通过
  $sql = "SELECT `number`, `title` FROM `trex_records` WHERE (`traineeNumber` = '{$traineeNumber}' OR `traineeName` = '{$traineeName}') AND `type` = 1 AND `year` = {$year} AND `pass` = '否'";
  $result = exec_sql($sql);
  $output['复训未通过'] = $result ? $result : [];

  # 其他已做
  $sql = "SELECT `number`, `title` FROM `trex_records` WHERE (`traineeNumber` = '{$traineeNumber}' OR `traineeName` = '{$traineeName}') AND `type` != 1 AND `year` = {$year}";
  $result = exec_sql($sql);
  $output['其他已做'] = $result ? $result : [];

  # 其他已通过
  $sql = "SELECT `number`, `title` FROM `trex_records` WHERE (`traineeNumber` = '{$traineeNumber}' OR `traineeName` = '{$traineeName}') AND `type` != 1 AND `year` = {$year} AND `pass` = '是'";
  $result = exec_sql($sql);
  $output['其他已通过'] = $result ? $result : [];

  # 其他未通过
  $sql = "SELECT `number`, `title` FROM `trex_records` WHERE (`traineeNumber` = '{$traineeNumber}' OR `traineeName` = '{$traineeName}') AND `type` != 1 AND `year` = {$year} AND `pass` = '否'";
  $result = exec_sql($sql);
  $output['其他未通过'] = $result ? $result : [];

  return $output;
}

function query_annual_exercises_by_office($year, $office, $detail = 0, $exclude = []) {
  $sql = "";
  $output = [];

  # 科室年度全部练习列表
  $sql = "SELECT `number`, `title`, `type` FROM `trex_exercises` WHERE `office` = '{$office}' AND `year` = {$year}";
  $result = exec_sql($sql);
  $output['科室年度全部练习列表'] = $result ? $result : [];

  // if ($detail === 1) {
  //   $sql = "SELECT * FROM `trex_exercises` WHERE `office` = '{$office}' AND `year` = {$year}";
  //   $result = exec_sql($sql);
  //   $output['科室年度全部练习列表_明细'] = $result ? $result : [];
  // }

  # 科室年度复训练习列表
  $sql = "SELECT `number`, `title`, `type` FROM `trex_exercises` WHERE `office` = '{$office}' AND `year` = {$year} AND `type` = 1";
  $result = exec_sql($sql);
  $output['科室年度复训练习列表'] = $result ? $result : [];

  # 科室年度其他练习列表
  $sql = "SELECT `number`, `title`, `type` FROM `trex_exercises` WHERE `office` = '{$office}' AND `year` = {$year} AND `type` != 1";
  $result = exec_sql($sql);
  $output['科室年度其他练习列表'] = $result ? $result : [];

  if (count($exclude) > 0) {}

  return $output;
}
